﻿function clearForm($form) {
    $form.find('input, select').each(function () {
        var $this = $(this);
        $this.val('');
        $this.removeAttr('checked');

    });
}

function extractFormData($form) {
    var data = {};
    $form.find('input, select').each(function() {
        var $this = $(this);
        if ($this.is('[type="checkbox"]')) {
        data[this.name] = $this.attr('checked') == 'checked';
        } else
            data[this.name] = $this.val();
    });

    return data;
}



//// Use this function to block the UI. Defaults to the saving block.
//function block($element, blockSelector) {
//    if (!blockSelector)
//        blockSelector = '#savingBlock';
//    if ($element)
//        $($element).block({ message: $(blockSelector), css: { 'font-size': '20px', width: '200px', height: '30px', 'line-height': '30px', border: '3px solid #000'} });
//    else
//        $.blockUI({ message: $(blockSelector), css: { 'font-size': '20px', width: '200px', height: '30px', 'line-height': '30px', border: '3px solid #000'} }).center();

//}

//// Use this function to unblock the UI.
//function unblock($element) {
//    if ($element)
//        $($element).unblock();
//    else
//        $.unblockUI();
//}

// Use this function to block the UI. Defaults to the saving block.
function block(options) {
    if (!options)
        options = {};
    if (!options.element)
        options.element = document.body;
    if (!options.text)
        options.text = 'Loading';

    var blockUIOptions = {
        message: '<img src="/Content/images/wait22trans.gif" /><span style="padding-left:10px;vertical-align:middle">' + options.text + '</span>',
        css: {
            border: 'none',
            padding: '15px',
            backgroundColor: '#fff',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            color: '#000',
            width: 'auto'
        }
    };

    if (options.element === document.body) {
        if (isBlocked)
            return;
        isBlocked = true;
    }

    $(options.element).block(blockUIOptions);
}

// Use this function to unblock the UI.
function unblock(options) {
    if (!options)
        options = {};
    if (!options.element)
        options.element = document.body;
    var blockOptions = {};
    if (options.callback) {
        if (options.callbackParameters)
            blockOptions.onUnblock = function () {
                options.callback(options.callbackParameters);
            };
        else
            blockOptions.onUnblock = options.callback;
    }

    if (options.element === document.body)
        if (!isBlocked) {
            if (options.callback)
                if (options.callbackParameters)
                    options.callback(options.callbackParameters);
                else
                    options.callback();
            return;
        } else {
            isBlocked = false;
        }
    $(options.element).unblock(blockOptions);
}
